package com.qyl.honghengshop.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.qyl.honghengshop.common.PageConstant;
import com.qyl.honghengshop.dto.User;
import com.qyl.honghengshop.service.AdminUserService;
import com.qyl.honghengshop.service.impl.AdminUserServiceImpl;
import com.qyl.honghengshop.utils.FileUpload;
import com.qyl.honghengshop.utils.MD5;
import com.qyl.honghengshop.utils.PageBean;


/**
 * 后台用户管理controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/user")
public class AdminUserController {

	private AdminUserService adminUserService = new AdminUserServiceImpl();
	
	//跳转到用户欢迎界面
	@RequestMapping("/toAdminWelcom")
	public String toAdminWelcom(){
		
		return "welcome";
	}
	
	//用户信息列表查询页面
	@RequestMapping("/userList")
	public String userList(Integer pageIndex,Integer pageSize,User user,ModelMap map){
			
		//1.计算总页数
		int recordCount = adminUserService.getRecordCountByUser(user);
		
		//2.创建pageBean
		PageBean pageBean = new PageBean(pageIndex, recordCount,pageSize);

		//调用service
		adminUserService.getUserList(pageBean,user);
		
		//把用户列表放到域中
		map.put("pageBean", pageBean);
		
		//回显用户输入
		map.put("userInput", user);
		
		return "user/userList";
	}
	
	//跳转到添加页面
	@RequestMapping("/toAdd")
	public String userAdd(User user){

		return "user/userAdd";
	}
	//用户添加功能
	@RequestMapping("/add")
	public String add(User user,ModelMap map) throws Exception{
		

		user.setCreate_date(new Date());
		
		String newPwd = MD5.getMD5(user.getPass_word());
		
		user.setPass_word(newPwd);
		
		//调用service添加用户
		int count = adminUserService.addUser(user);
		if(count>0){
			map.put(PageConstant.TIP, "添加成功");			
		}else{
			map.put(PageConstant.TIP, "添加失败");
		}
		
		return "user/userAdd";
	}
	//用户删除功能
	@RequestMapping("/delUser")
	public String delUser(Integer id,ModelMap map){
		
		adminUserService.delUser(id);
		
		map.addAttribute(PageConstant.TIP, "删除成功");
	
		return "forward:userList";
	}
	@RequestMapping("/toUserUpdate")
	public String toUpdate(Integer id,ModelMap map){
		
		User updateUser = adminUserService.getUserById(id);
		
		map.put("updateUser", updateUser);
		
		return "user/userUpdate";
	}
	@RequestMapping("/update")
	public String update(User user,ModelMap map){
		
		int count = adminUserService.update(user);
		
		if(count>0){
			
			map.put(PageConstant.TIP, "修改成功");
		}else{
			
			map.put(PageConstant.TIP, "修改失败");
		}		
		return "forward:toUserUpdate";
	}
	@RequestMapping("/delBatchUser")
	public String delBatchUser(int[] ids,ModelMap map){
		
		int count = adminUserService.delBatchUser(ids);
		
		if(count >0){
			map.put(PageConstant.TIP, "删除成功");
		}else{
			map.put(PageConstant.TIP, "删除失败");
		}
		
		return "forward:userList";
	}
	@RequestMapping("/userPicAjax")
	@ResponseBody
	public Map<String, String> userPicAjax(MultipartFile headImg,HttpSession session) throws Exception{
		
		Map<String, String> map = new HashMap<String, String>();
		
		String fileName = FileUpload.fileUpload(headImg);
		
		map.put("fileName", fileName);
		
		map.put("status", "success");
		
		User user = (User) session.getAttribute(PageConstant.SESSION_USER);
		
		user.setUserHeadImg(fileName);
		
		adminUserService.updateUserHead(user);
		
		return map;
	}
	
	
}
